clear all
set more off
cd /Users/zimaoxiao/Dropbox/CC_Yield_Predict/replication_package/  // Navigate to replication folder on your own machine

qui{

* ============================
* 	  Panel A: 2020 PWMSE
* ============================	
* prepare the data for regression
use if inrange(year,1950,2000) using data/weather/weather_processed_current, clear
merge 1:1 county_fips year using data/yield/yields_1950_2022, keep(3) keepusing(corn_yield) nogenerate
gen lncornyield=log(corn_yield)
gen state_fips=floor(county_fips/1000)

rename (gdd8_32 gdd8_32_sq hdd34_sqrt) (shf_gdd8_32 shf_gdd8_32_sq shf_hdd34_sqrt)
rename (gdd0_29 gdd29) (one_gdd0_29 one_gdd29)
rename (gdd0_24 gdd24_26 gdd26) (two_gdd0_24 two_gdd24_26 two_gdd26)
local temp_spec 
local temp_spec `temp_spec'	tave_m*
local temp_spec `temp_spec'	dd1bin*
local temp_spec `temp_spec'	timeInt*
local temp_spec `temp_spec'	shf*
local temp_spec `temp_spec'	one*
local temp_spec `temp_spec'	two*

tempfile DataReg
save `DataReg', replace

foreach i in `temp_spec' {
	noi di "Temperature spec: `i' for 2020 PWMSE"
	use `DataReg', clear
	noi get_pwmse using data/weather/saved_norms_2020, yvar(lncornyield) xvar(`i' prec prec2) trends(i.state_fips#c.year##c.year) unit(county_fips) time(year) t(2000) train_ratio(0.75) num_simulations(1000) norms(N D1 D2 M1 M2 Y1 Y2) h(10) seed(36369) quiet
}

* ============================
* 	 Panel B: 2050 PWMSE (i)
* ============================	
* prepare the data for regression
use if inrange(year,1950,2020) using data/weather/weather_processed_current, clear
merge 1:1 county_fips year using data/yield/yields_1950_2022, keep(3) keepusing(corn_yield) nogenerate
gen lncornyield=log(corn_yield)
gen state_fips=floor(county_fips/1000)

rename (gdd8_32 gdd8_32_sq hdd34_sqrt) (shf_gdd8_32 shf_gdd8_32_sq shf_hdd34_sqrt)
rename (gdd0_29 gdd29) (one_gdd0_29 one_gdd29)
rename (gdd0_24 gdd24_26 gdd26) (two_gdd0_24 two_gdd24_26 two_gdd26)
local temp_spec 
local temp_spec `temp_spec'	tave_m*
local temp_spec `temp_spec'	dd1bin*
local temp_spec `temp_spec'	timeInt*
local temp_spec `temp_spec'	shf*
local temp_spec `temp_spec'	one*
local temp_spec `temp_spec'	two*

tempfile DataReg
save `DataReg', replace

foreach i in `temp_spec' {
	use `DataReg', clear
	noi di "Temperature spec: `i' for 2050 PWMSE under SSP2-4.5"
	noi get_pwmse using data/weather/saved_norms_ssp245, yvar(lncornyield) xvar(`i' prec prec2) trends(i.state_fips#c.year##c.year) unit(county_fips) time(year) t(2000) train_ratio(0.75) num_simulations(1000) norms(N D1 D2 M1 M2 Y1 Y2) h(10) seed(99996) quiet
}

* ============================
* 	Panel C: 2050 PWMSE (ii)
* ============================	
foreach i in `temp_spec' {
	use `DataReg', clear
	noi di "Temperature spec: `i' for 2050 PWMSE under SSP5-8.5"
	noi get_pwmse using data/weather/saved_norms_ssp585, yvar(lncornyield) xvar(`i' prec prec2) trends(i.state_fips#c.year##c.year) unit(county_fips) time(year) t(2000) train_ratio(0.75) num_simulations(1000) norms(N D1 D2 M1 M2 Y1 Y2) h(10) seed(66669) quiet
}

erase yourdataset.dta
}

*** EOF
